//
//  LoginViewController.m
//  duoduo
//
//  Created by wanghao on 16/10/12.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "ForgetViewController.h"
#import "LXTableBarController.h"
#import "HYPersonModel.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UIButton *forgetBtn;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"用户登录";
    [self setRightItem];
    [self initUI];

}

-(void)setRightItem
{
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];

    [btn setTitle:@"注册" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:18];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setContentEdgeInsets:UIEdgeInsetsMake(2, 12, 0, -12)];
    [btn addTarget:self action:@selector(clickRightItem) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
}

- (void)initUI
{
    self.view.backgroundColor = LXHexColor(0xF5F6F7);

    //忘记密码按钮
    [_forgetBtn setTitleColor:LXHexColor(0x5DCA68) forState:UIControlStateNormal];

    [_forgetBtn addTarget:self action:@selector(forgetClick) forControlEvents:UIControlEventTouchUpInside];

    //登录按钮
    _loginBtn.backgroundColor = LXHexColor(0x5DCA68);
    _loginBtn.layer.cornerRadius = 6;
    _loginBtn.layer.masksToBounds = YES;
    [_loginBtn addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
}
//去注册
- (void)clickRightItem
{
    RegisterViewController * registerVC = [[RegisterViewController alloc]init];
    [self.navigationController pushViewController:registerVC animated:YES];
}

//去登录
- (void)loginClick
{
    if (!_phoneField.text.length) {
        ShowSomeMsg(@"请输入手机号码");
        return;
    }
    if (!_passwordField.text.length) {
        ShowSomeMsg(@"请输入密码");
        return;
    }
    __weak typeof(self) weakSelf = self;
    NSDictionary *dic = @{@"acc":_phoneField.text,
                          @"pwd":_passwordField.text};
    [MHNetworkManager postReqeustWithURL:URL(@"?m=appface&c=user&a=login") params:dic successBlock:^(NSDictionary *returnData) {
        NSLog(@"returnData = %@",returnData);
        if (ResultJudge) {
            HYPersonModel *model = [[HYPersonModel alloc] init];
            [model setValuesForKeysWithDictionary:returnData[@"data"]];
            [[TMCache sharedCache] setObject:model forKey:@"userModel"];
            [[TMCache sharedCache] setObject:model.uid forKey:@"userId"];

            LXTableBarController * tabbar = [[LXTableBarController alloc]init];
            [weakSelf presentViewController:tabbar animated:YES completion:nil];
        }else{
            ShowResultMsg;
        }
    } failureBlock:^(NSError *error) {
        DTLog(@"error = %@",error);
        ShowFailMsg;
    } showHUD:YES];
}

//忘记密码
- (void)forgetClick
{
    ForgetViewController * forgetVC = [[ForgetViewController alloc]init];
    [self.navigationController pushViewController:forgetVC animated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
